﻿Imports System.Math
Public Class OpenGLCamera
    Sub New()
        Reset()
    End Sub
    Public Structure T3DPoint
        Dim x As Double
        Dim y As Double
        Dim z As Double
        Sub New(ByVal tx As Double, ByVal ty As Double, ByVal tz As Double)
            x = tx
            y = ty
            z = tz
        End Sub
    End Structure

    Public pos As T3DPoint
    Public rot As T3DPoint
    Public d As T3DPoint
    Public xD As Single = 0.0
    Public yD As Single = 0.0
    Public Spd As Double = 1.0F
    Public Property Speed() As Double
        Get
            Return Spd
        End Get
        Set(value As Double)
            Spd = value * 0.6
        End Set
    End Property
    Public Sub Reset()
        pos = New T3DPoint(0, 0, 0)
        rot = New T3DPoint(0, 0, 0)
        d = New T3DPoint(0, 0, 0)
    End Sub
    Public Sub Update(Optional ByVal ms As MouseStatus = Nothing, _
                      Optional ByVal kb As KeyStatus = Nothing, _
                      Optional ByVal xON As Boolean = True, Optional ByVal yON As Boolean = True)

        If Not IsNothing(ms) Then
            With ms
                .diff.X = (.newPos.X - .oldPos.X)
                .diff.Y = (.newPos.Y - .oldPos.Y)

                If .msLeft Then
                    If xON Then
                        If .newPos.X <> .oldPos.X Then
                            rot.x += .diff.X * Spd
                        End If
                    End If
                    If yON Then
                        If .newPos.Y <> .oldPos.Y Then
                            rot.y += .diff.Y * Spd
                        End If
                    End If

                    If rot.x >= 360.0F Then
                        rot.x = 0.0F
                    ElseIf rot.x <= -360.0F Then
                        rot.x = 0.0F
                    End If
                    If rot.y >= 90.0F Then
                        rot.y = 90.0F
                    ElseIf rot.y <= -90.0F Then
                        rot.y = -90.0F
                    End If

                    With d
                        .x = rot.x * piRad
                        .y = rot.y * piRad
                    End With
                End If
                If .msRight Then
                    If yON Then
                        If .newPos.Y <> .oldPos.Y Then
                            pos.y += .diff.Y * Spd * 0.4
                        End If
                    End If
                    If xON Then
                        If .newPos.X < .oldPos.X Then
                            pos.x += Cos(d.x) * Spd * 0.4
                            pos.z += Sin(d.x) * Spd * 0.4
                        ElseIf .newPos.X > .oldPos.X Then
                            pos.x -= Cos(d.x) * Spd * 0.4
                            pos.z -= Sin(d.x) * Spd * 0.4
                        End If
                    End If
                End If
            End With
        End If
        If Not IsNothing(kb) Then
            With kb
                If .W Then
                    If rot.y = 90.0F Or rot.y = -90.0F Then
                        pos.y += Sin(d.y) * Spd
                    Else
                        pos.x -= Sin(d.x) * Spd
                        pos.z += Cos(d.x) * Spd
                        pos.y += Sin(d.y) * Spd
                    End If
                ElseIf .S Then
                    If rot.y = 90.0F Or rot.y = -90.0F Then
                        pos.y -= Sin(d.y) * Spd
                    Else
                        pos.x += Sin(d.x) * Spd
                        pos.z -= Cos(d.x) * Spd
                        pos.y -= Sin(d.y) * Spd
                    End If
                End If
                If .A Then
                    pos.x += Cos(d.x) * Spd
                    pos.z += Sin(d.x) * Spd
                ElseIf .D Then
                    pos.x -= Cos(d.x) * Spd
                    pos.z -= Sin(d.x) * Spd
                End If
            End With
        End If
    End Sub
End Class
